package com.yubest.junit.thread;

import java.util.concurrent.CyclicBarrier;

/**
 * CyclicBarrier
 *
 * @Author: hweiyu
 * @Date: 2022/11/15 15:54
 */
public class MyCyclicBarrier extends Thread {

    private CyclicBarrier cyclicBarrier;

    public MyCyclicBarrier(String name, CyclicBarrier cyclicBarrier) {
        super(name);
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            Sleep.seconds(1);
            System.out.println(getName() + " 到达栅栏 A");
            cyclicBarrier.await();
            System.out.println(getName() + " 冲破栅栏 A");

            Sleep.seconds(1);
            System.out.println(getName() + " 到达栅栏 B");
            cyclicBarrier.await();
            System.out.println(getName() + " 冲破栅栏 B");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
